<template>
    <orh-table reload  @on-reload="refresh"
        pages :pageDate="pageDate"
        @on-page-change="queryData"
        search  @on-search="searchmt"
        :searchOption="typeliat" :searchOptionSelected="searchOptionSelected">
        <Select v-model="statu" @on-change="formState" style="width:200px" slot="option">
            <Option v-for="item in newstype" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
        <Table :columns="columns" :data="data" :loading="tableload" slot="table"></Table>
        <orh-sidebar v-model="show" :title="titles" scale slot="others">
            <component :is="content" :dataId="dataIds" @requery="requery" :datalists="datalists"></component>
        </orh-sidebar>
    </orh-table>
</template>

<script>

export default {
    components: {
        Edit: resolve => {require(['./Edit.vue'], resolve);},
        Detail: resolve => {require(['./Detail.vue'], resolve);},
    },
    name: 'leave-leavepending',
    data () {
        return {
            statu:null,
            datalists:'',
            show:false,
            content:null,
            titles:null,
            tableload:true,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            listurl:null,
            dataIds:null,
            data:[],
            searchOptionSelected: "teacher_name",
            typeliat: [
                {
                    value: 'teacher_name' ,
                    label: '教师姓名',
                }
            ],
            newstype:[
                {
                    value:'no_examine',
                    label:'未审核'
                },
                {
                    value:'examine_adopt',
                    label:'通过'
                },
                {
                    value:'examine_noadopt',
                    label:'未通过'
                },
            ],
            columns:[
                {
                    title: '教师姓名',
                    align:'center',
                    render: (h, params) => {
                        return h('div', params.row.teacher_leave.teacher!=undefined?params.row.teacher_leave.teacher.teacher_name.zh:'');
                    }
                },
                {
                    title: '请假种类',
                    align:'center',
                    render: (h, params) => {
                        return h('div', params.row.teacher_leave.teacher_leave_kinds[0].dictionary_name.zh);
                    }
                },
                {
                    title: '请假开始时间',
                    align:'center',
                    render: (h, params) => {
                        return h('div', params.row.teacher_leave.start_time);
                    }
                },
                {
                    title: '请假结束时间',
                    align:'center',
                    render: (h, params) => {
                        return h('div', params.row.teacher_leave.end_time);
                    }
                },
                {
                    title: '是否调课',
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('div',params.row.teacher_leave.whether_class == '1'?'是':'否')
                        ]);
                    }
                },
                {
                    title: '教师请假原因',
                    align:'center',
                    render: (h, params) => {
                        return h('div', params.row.teacher_leave.teacher_leave_reason.zh);
                    }
                },
                {
                    title: '审核状态',
                    align:'center',
                    render: (h, params) => {
                        let col='';
                        let name='';
                        if(params.row.status== 1){
                            col='#1aad33';
                            name='通过';
                        }else if(params.row.status== 2){
                            col='#f00';
                            name='未通过';
                        }else if(params.row.status== 3){
                            col='#1aad33';
                            name='同意并向上级审批';
                        }else{
                            col='#007d7f';
                            name='未审核';
                        }
                        return h('div', [
                            h('div',{
                                style: {
                                color: col,
                                },
                            }, name)
                        ]);
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        let aa = this.statu
                        let name = ''
                        if(aa == 'examine_adopt' || aa == 'examine_noadopt'){
                            name = '修改'
                        }else{
                            name = '审核'
                        }
                            return h('div', [
                            h('Button', {
                                props: {
                                    type: 'success',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.see(params.row)
                                    }
                                }
                            }, '查看'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.leaveData(params.row)
                                    }
                                }
                            }, name),
                        ]);
                    }
                },
            ],
        }
    },

    methods: {
        /************************************************************
         * 重新请求数据
         ************************************************************/
        requery(state){
            if(state == true){
                this.show = false
                this.tableload = true
                this.listurl=`api/leave/v1/teacher_leave/my_examine?examine_type=${this.statu}&page=`
                this.queryData(this.pageDate.current_page)
            }
        },
        /************************************************************
         * 刷新数据
         ************************************************************/
        refresh(){
            this.listurl=`api/leave/v1/teacher_leave/my_examine?examine_type=${this.statu}&page=`
            this.queryData(this.pageDate.current_page)
        },
        /************************************************************
         * 审核
         ************************************************************/
        leaveData(index){
            this.datalists = index
            this.titles = '审核'
            this.show = true
            this.content = 'Edit'
        },
        /************************************************************
         * 查看
         ************************************************************/
        see(data){
            this.datalists = data
            this.titles = '查看详情'
            this.show = true
            this.content = 'Detail'
        },
        /************************************************************
         * 待批请假分类
         ************************************************************/
        formState(val){
            this.statu = val
            this.tableload = true
            this.listurl=`api/leave/v1/teacher_leave/my_examine?examine_type=${this.statu}&page=`
            this.queryData(this.pageDate.current_page)
        },
        /************************************************************
         * 获取数据列表
         ************************************************************/
        queryData(index){
            this.tableload = true
            this.$http.get(`${this.listurl}${index}`).then(val=>{
                this.data = val.data.data
                if(val.pages!=null){
                    this.pageDate.per_page = val.data.per_page
                    this.pageDate.total = val.data.total
                    this.pageDate.current_page = val.data.current_page
                }
                this.tableload = false
            }).catch(err => {
				if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
				this.tableload = false;
			})
        },
        /************************************************************
         * 默认第一个审核状态
         ************************************************************/
        examineData(){
            this.statu = this.newstype[0].value
            this.listurl=`api/leave/v1/teacher_leave/my_examine?examine_type=${this.statu}&page=`
            this.queryData(this.pageDate.current_page)
        },
        // 搜索操作
        searchmt(str,val) {
            if(str!=''){
                this.listurl = `api/leave/v1/teacher_leave/my_examine?examine_type=${this.statu}&searchtype=${val}&searchvalue=${str}&page=`
                this.queryData(this.pageDate.current_page)
            }
        },
    },
    mounted(){
        this.examineData()
    },
}
</script>
